package  
{
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.masks.Pixelmask;
	import net.flashpunk.utils.Input;
	import net.flashpunk.Sfx;
	/**
	 * ...
	 * @author Donatello
	 */
	public class ShootGuy extends Entity
	{
		[Embed(source = "img/sheets/shootSheet.png")] private const PLAYER_ANIM:Class;
		[Embed(source = "img/sheets/shootCell.png")] private const PLAYER_PIX:Class;
		
		[Embed(source = "img/sheets/shootSheetEnemy.png")] private const PLAYER_ANIM_E:Class;
		[Embed(source = "img/sheets/shootCellEnemy.png")] private const PLAYER_PIX_E:Class;
		
		[Embed(source = "sfx/shot.mp3")] private static const SHOT:Class;
		private var shot:Sfx= new Sfx(SHOT);
		
		
		private var playerAnim:Spritemap;
		private var good:Boolean;
		private var pressed:Boolean = false;
		
		private var bullets:Vector.<Bullet>;
		private var bulletAux:Bullet;
		public var state:String = "walking";
	
		private var speed:Number;
		private var time:Number=1.0;
		
		public function ShootGuy(px:Number=305,py:Number=500,vel:Number=1,isgood:Boolean=false) 
		{
			good = isgood;
			
			if (good)
			{
				playerAnim = new Spritemap(PLAYER_ANIM, 69,147);
				mask = new Pixelmask(PLAYER_PIX);
				type = "Player";
				
				playerAnim.flipped = true;
			}
			else
			{
				playerAnim = new Spritemap(PLAYER_ANIM_E, 69,147);
				mask = new Pixelmask(PLAYER_PIX_E);
				type = "Enemy";
				playerAnim.add("dying", [4, 5, 6, 7], 10, false);
				
				
				
			}
			
			speed = vel;
			bullets = new Vector.<Bullet>;
			
			playerAnim.add("walking", [0,1,2,3], 10, true);
			
			graphic = playerAnim;
					
			x = px;
			y = py;
			
			playerAnim.play("walking");
			
			shot.volume = 0.2;
			
		}
		
		
		override public function update():void
		{
			time += FP.elapsed;
			
			if (good)
			{
				if(Input.mousePressed && !pressed)
				{
					if (time >= 1)
					{
						pressed = true;
						bulletAux = new Bullet(x-40, y+54,-10);
						bullets.push(bulletAux);
						FP.world.add(bulletAux)
						time = 0.0;
						shot.play();
					}
				}
				
				if (Input.mouseReleased)
				{	
					pressed = false;
				}
				
				
			}
			else
			{
				if (FP.rand(1000) < 20  && state!="dying"  && x>=0)
				{
					if (time >= 1)
					{
						
						bulletAux = new Bullet(x +75, y + 54, 10,"EnemyBullet");
						bullets.push(bulletAux);
						FP.world.add(bulletAux);
						time = 0.0;
						shot.play();
					}
				}
				
				if (collide("Bullet", x, y) && state=="walking")
				{
					morir();
					FP.world.remove(collide("Bullet", x, y));
				}
						
			}
			
			if(good)
				x -= speed;
			else if(state!="dying")
			{				
				x += speed;
			}
		}
		
		
		public function morir():void
		{
			
				if (!good)
				{	
					state = "dying";
					playerAnim.play(state);
				}
		}
	}

}